﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HuynhQuangWebsite.Models;
using System.IO;
using THS.HtmlHelpers;
namespace HuynhQuangWebsite.Controllers
{
    public class ManageProjectController : BaseController
    {
        huynhquangco_websiteEntities ctx = new huynhquangco_websiteEntities();
        //
        // GET: /Manageprojects/
        int PageSize = 6;
        public ActionResult IndexProject(int page = 1)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            var model = new HomeAdminListViewModel();
            IList<Project> projects = ctx.Projects.OrderByDescending(p => p.ProjectID).ToList();
            model.ProjectList = projects.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.ProjectInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = projects.Count() };
            return View(model);
        }
        public ActionResult Paging(int page = 1)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            var model = new HomeAdminListViewModel();
            IList<Project> projects = ctx.Projects.OrderByDescending(p => p.ProjectID).ToList();
            model.ProjectList = projects.Skip((page-1) * PageSize).Take(PageSize).ToList();
            model.ProjectInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = projects.Count() };
            return PartialView("UCProjectList", model);
        }
        public ActionResult Detail(int id)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            var project = (from projects in ctx.Projects where projects.ProjectID == id select projects).SingleOrDefault();
            if (project != null)
                return View(project);
            else
            {
                project = new Models.Project();
                return View(project);
            }
            //return trang thông báo lỗi
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Detail(int id, HttpPostedFileBase uploadFile)
        {
            try
            {
                // TODO: Add update logic here
                // if (!IsAuthenticateAdmin())
                //   return RedirectToAction("LogOn", "AdminHome");
                Project project = ctx.Projects.Where(p => p.ProjectID == id).SingleOrDefault();

                if (uploadFile != null)
                {
                    String oldImage = project.ProjectImg;
                    String oldThumbImage = project.ProjectImgThumb;
                    var fileName = Path.GetFileName(uploadFile.FileName);
                    String sFileName = Utilities.GenerateImageName(Request, Utilities.VirtualPath, fileName, 1.ToString());
                    string filePath = Path.Combine(Request.MapPath(Utilities.VirtualPath), sFileName);
                    String fileThumbPath = Path.Combine(Request.MapPath(Utilities.VirtualPath + "Thumbs/"), sFileName);
                    MakeImagesAndThumbFile(uploadFile.InputStream, filePath, fileThumbPath, 400, 300, 180, 180, 75);
                    project.ProjectImg = Utilities.PhysicalPath + sFileName;
                    project.ProjectImgThumb = String.Format("{0}Thumbs/{1}", Utilities.PhysicalPath, sFileName);
                    //delete image
                    DeleteFile(Request, Utilities.VirtualPath, oldImage);
                    DeleteFile(Request, Utilities.VirtualPath + "Thumbs", oldThumbImage);
                }

                UpdateModel(project);
                ctx.SaveChanges();
                ViewData["Msg"] = "Đã cập nhật!";
                return View(project);
                //return RedirectToAction("Indexproject");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                // TODO: Add delete logic here
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOn", "AdminHome");
                Project project = ctx.Projects.Where(p => p.ProjectID == id).SingleOrDefault();
                String oldImage = project.ProjectImg;
                String oldThumbImage = project.ProjectImgThumb;
                ctx.Projects.DeleteObject(project);
                ctx.SaveChanges();
                DeleteFile(Request, Utilities.VirtualPath, oldImage);
                DeleteFile(Request, Utilities.VirtualPath + "Thumbs", oldThumbImage);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /FunRingManagement/Create

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create([Bind(Exclude = "ProjectID")]Project project, HttpPostedFileBase uploadFile)
        {
            // TODO: Add update logic here
            // if (!IsAuthenticateAdmin())
            //   return RedirectToAction("LogOn", "AdminHome");
            try
            {
                if (uploadFile != null)
                {
                    var fileName = Path.GetFileName(uploadFile.FileName);
                    String sFileName = Utilities.GenerateImageName(Request, Utilities.VirtualPath, fileName, 1.ToString());
                    string filePath = Path.Combine(Request.MapPath(Utilities.VirtualPath), sFileName);
                    String fileThumbPath = Path.Combine(Request.MapPath(Utilities.VirtualPath + "Thumbs/"), sFileName);
                    MakeImagesAndThumbFile(uploadFile.InputStream, filePath, fileThumbPath, 400, 300, 130, 130, 75);
                    project.ProjectImg = Utilities.PhysicalPath + sFileName;
                    project.ProjectImgThumb = String.Format("{0}Thumbs/{1}", Utilities.PhysicalPath, sFileName);
                }
                ctx.AddToProjects(project);
                ctx.SaveChanges();
                ViewData["Msg"] = "Đã tạo thành công!";
                return View();
            }
            catch
            {
                return View();

            }
        }
    }
}
